<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace log;

use think\Db;
use think\Request;
use think\App;

/**
 * 本地化调试输出到文件
 */
class Mysql
{

    /**
     * 日志写入接口
     * @access public
     * @param array $log 日志信息
     * @return bool
     */
    public function save(array $log = [])
    {
        $info = $this->info($log);
//        var_dump($info);exit;
        Db::name("log_visitor")->insert($info);
        return true;
    }

    protected function info($log, $apart = false)
    {
        $info = [];
        $request = Request::instance();
        //获取当前IP；
        $info['ip'] = $request->ip();
        //获取当前ip信息；
        $data = $this->get_ip_from();
        //获取出网ip
        $info['ips'] = $data['ip'];
        //获取出网ip国家
        $info['country'] = $data['country'];
        //获取出网ip地区
        $info['area'] = $data['area'];
        //获取出网ip省
        $info['region'] = $data['region'];
        //获取出网ip市
        $info['city'] = $data['city'];
        //获取出网ip县
        $info['county'] = $data['county'];
        //请求参数
        $info['param'] = json_encode($request->param());
        //资源类型
        $info['type'] = $request->type();
        //请求方法
        $info['method'] = $request->method();
        //是否ajax请求
        $info['isAjax'] = var_export($request->isAjax(), true);
        //当前模块名称
        $info['module'] = $request->module();
        //当前控制器名称
        $info['controller'] = $request->controller();
        //当前操作名称
        $info['action'] = $request->action();
        //获取当前域名
        $info['domain'] = $request->domain();
        // 获取URL地址中的后缀信息
        $info['ext'] = $request->ext();
        // 获取当前URL地址 不含QUERY_STRING
        $info['baseUrl'] = $request->baseUrl();
        //获取访问浏览器
        $info['browser'] = $this->get_browser();
        //获取浏览器语言
        $info['lang'] = $this->get_lang();
        //获取当前操作系统
        $info['os'] = $this->get_os();
        if (!IS_CLI) {
            if (App::$debug && !$apart) {
                // 获取基本信息
                if (isset($_SERVER['HTTP_HOST'])) {
                    $info['current_url'] = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                } else {
                    $info['current_url'] = "cmd:" . implode(' ', $_SERVER['argv']);
                }
                //运行时间
                $runtime = round(microtime(true) - THINK_START_TIME, 10);
                $info['runtime'] = number_format($runtime, 6) . 's';
                //吞吐率
                $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
                $info['reqs'] = $reqs . 'req/s';
                //内存消耗
                $memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
                $info['memory_use'] = $memory_use . 'kb';
                //文件加载数量
                $file_load = count(get_included_files());
                $info['file_load'] = $file_load;
            }
        }

        $info['sql_info'] = "";
        if (isset($log['sql'])) {
            $info['sql_info'] = json_encode($log['sql']);
        }

        $info['error_type'] = "";
        $info['error_info'] = "";
        if (isset($log['error'])) {
            //错误信息
            $info['error_type'] = "error";
            $info['error_info'] = json_encode($log['error']);
        }
        $info['time'] = time();
        return $info;
    }


    //获取浏览器类型
    function get_browser()
    {
        $user_OSagent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "Maxthon(Microsoft IE)";
        } elseif (strpos($user_OSagent, "Maxthon 2.0")) {
            $visitor_browser = "Maxthon 2.0";
        } elseif (strpos($user_OSagent, "Maxthon")) {
            $visitor_browser = "Maxthon";
        } elseif (strpos($user_OSagent, "Edge")) {
            $visitor_browser = "Edge";
        } elseif (strpos($user_OSagent, "Trident")) {
            $visitor_browser = "IE";
        } elseif (strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "IE";
        } elseif (strpos($user_OSagent, "MSIE")) {
            $visitor_browser = "MSIE 较高版本";
        } elseif (strpos($user_OSagent, "NetCaptor")) {
            $visitor_browser = "NetCaptor";
        } elseif (strpos($user_OSagent, "Netscape")) {
            $visitor_browser = "Netscape";
        } elseif (strpos($user_OSagent, "Chrome")) {
            $visitor_browser = "Chrome";
        } elseif (strpos($user_OSagent, "Lynx")) {
            $visitor_browser = "Lynx";
        } elseif (strpos($user_OSagent, "Opera")) {
            $visitor_browser = "Opera";
        } elseif (strpos($user_OSagent, "MicroMessenger")) {
            $visitor_browser = "微信浏览器";
        } elseif (strpos($user_OSagent, "Konqueror")) {
            $visitor_browser = "Konqueror";
        } elseif (strpos($user_OSagent, "Mozilla/5.0")) {
            $visitor_browser = "Mozilla";
        } elseif (strpos($user_OSagent, "Firefox")) {
            $visitor_browser = "Firefox";
        } elseif (strpos($user_OSagent, "U")) {
            $visitor_browser = "Firefox";
        } else {
            $visitor_browser = "其它";
        }
        return $visitor_browser;
    }

//    //获得访客浏览器类型
//    protected function get_browser() {
//        if(empty($_SERVER['HTTP_USER_AGENT'])){
//            return 'robot！';
//        }
//        if((false == strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))&&(strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')!==FALSE)){
//            return 'Internet Explorer 11.0';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 10.0')){
//            return 'Internet Explorer 10.0';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
//            return 'Internet Explorer 9.0';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
//            return 'Internet Explorer 8.0';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
//            return 'Internet Explorer 7.0';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
//            return 'Internet Explorer 6.0';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){
//            return 'Edge';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
//            return 'Firefox';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
//            return 'Chrome';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
//            return 'Safari';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
//            return 'Opera';
//        }
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
//            return '360SE';
//        }
//        //微信浏览器
//        if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessage')){
//            return 'MicroMessage';
//        }
//        return "unknow";
//    }

    //获得访客浏览器语言
    protected function get_lang()
    {
        if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
            $lang = substr($lang, 0, 5);
            if (preg_match("/zh-cn/i", $lang)) {
                $lang = "简体中文";
            } elseif (preg_match("/zh/i", $lang)) {
                $lang = "繁体中文";
            } else {
                $lang = "English";
            }
            return $lang;
        } else {
            return "unknow";
        }
    }

    //获取访客操作系统
    protected function get_os()
    {
        if (!empty($_SERVER['HTTP_USER_AGENT'])) {
            $OS = $_SERVER['HTTP_USER_AGENT'];
            if (preg_match('/win/i', $OS)) {
                $OS = 'Windows';
            } elseif (preg_match('/mac/i', $OS)) {
                $OS = 'MAC';
            } elseif (preg_match('/linux/i', $OS)) {
                $OS = 'Linux';
            } elseif (preg_match('/unix/i', $OS)) {
                $OS = 'Unix';
            } elseif (preg_match('/bsd/i', $OS)) {
                $OS = 'BSD';
            } else {
                $OS = 'Other';
            }
            return $OS;
        } else {
            return "unknow";
        }
    }

    //根据ip信息
    protected function get_ip_from($ip = "myip")
    {
        $ip_json = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip);//根据taobao ip
        $ip_arr = json_decode($ip_json, true);
        if ($ip_arr['code'] == 0) {
            return $ip_arr['data'];
        } else {
            return "";
        }
    }
}
